package com.java.TCP.upload_file_thread;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);

        String path = "网络编程\\com\\java\\TCP\\upload_file_thread\\files\\syx.jpg";
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());

        // 将文件内容发送给服务端
        int length;
        byte[] buffer = new byte[1024];
        while ((length = bufferedInputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer, 0, length);
        }
        socket.shutdownOutput();

        // 接收服务端上传成功或失败的反馈
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String receive = "";
        while ((receive = bufferedReader.readLine()) != null) {
            System.out.println(receive);
        }
        socket.shutdownInput();

        socket.close();
    }
}
